package com.medical.admin.patient.service.Impl;

import com.medical.admin.patient.common.dto.BeHospitalizedDto;
import com.medical.admin.cost.common.dto.DetailInfo;
import com.medical.admin.cost.common.dto.IndentDto;
import com.medical.admin.patient.common.dto.PatientDto;
import com.medical.admin.patient.entity.BeHospitalized;
import com.medical.admin.cost.entity.Indent;
import com.medical.admin.patient.mapper.BeHospitalizedMapper;
import com.medical.admin.cost.mapper.IndentMapper;
import com.medical.admin.patient.service.BeHospitalizedService;
import com.medical.admin.patient.service.PatientService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class BeHospitalizedServiceImpl implements BeHospitalizedService {
    @Resource
    private PatientService patientService;
    @Resource
    private BeHospitalizedMapper beHospitalizedMapper;
    @Resource
    private IndentMapper indentMapper;

    @Override
    public BeHospitalizedDto searchHospitalized(Long patientId) {
        //判读患者是否存在
        PatientDto patientDto = patientService.researchPatient(patientId);
        if (ObjectUtils.isEmpty(patientDto)) {
            throw new ServiceException(ResponseCode.PATIENT_NULL_ERROR);
        }
        //根据患者id查询该患者的住院信息
        BeHospitalized beHospitalized = beHospitalizedMapper.selectByPatientId(patientId);
        if (ObjectUtils.isEmpty(beHospitalized)) {
            throw new ServiceException(ResponseCode.HOSPITALIZED_SELECT_ERROR);
        }
        return BeanConvertUtils.convertTo(beHospitalized, BeHospitalizedDto::new);
    }

    @Override
    public Integer addHospitalized(BeHospitalizedDto beHospitalizedDto) {
        int count = beHospitalizedMapper.insert(BeanConvertUtils.convertTo(beHospitalizedDto, BeHospitalized::new));
        if (count <= 0) {
            throw new ServiceException(ResponseCode.HOSPITALIZED__ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyHospitalized(BeHospitalizedDto beHospitalizedDto) {
        int count = beHospitalizedMapper.updateById(BeanConvertUtils.convertTo(beHospitalizedDto, BeHospitalized::new));
        if (count <= 0) {
            throw new ServiceException(ResponseCode.HOSPITALIZED_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeHospitalized(Long patientId) {
        int count = beHospitalizedMapper.deleteByPatientId(patientId);
        if (count <= 0) {
            throw new ServiceException(ResponseCode.HOSPITALIZED_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public DetailInfo searchHospitalizedDetail(Long indentId) {
        //根据订单id查询订单
        Indent indent = indentMapper.selectById(indentId);
        if (ObjectUtils.isEmpty(indent)) {
            throw new ServiceException(ResponseCode.INDENT_SELECT_ERROR);
        }
        //根据patientId查询患者信息
        PatientDto patientDto = patientService.researchPatient(indent.getPatientId());
        if (ObjectUtils.isEmpty(patientDto)) {
            throw new ServiceException(ResponseCode.PATIENT_NULL_ERROR);
        }
        if (patientDto.getState() == 0){
            throw new ServiceException(ResponseCode.PATIENT_STATE_ERROR);
        }
        //根据患者id查询该患者的住院信息
        BeHospitalizedDto beHospitalizedDto = this.searchHospitalized(patientDto.getPatientId());
        if (ObjectUtils.isEmpty(beHospitalizedDto)) {
            throw new ServiceException(ResponseCode.HOSPITALIZED_SELECT_ERROR);
        }
        DetailInfo detailInfo = new DetailInfo();
        detailInfo.setPatientDto(patientDto);
        detailInfo.setIndentDto(BeanConvertUtils.convertTo(indent, IndentDto::new));
        detailInfo.setBeHospitalizedDto(beHospitalizedDto);

        return detailInfo;
    }
}
